/* eslint-disable */
import axios from 'axios'
import qs from 'qs'
import {getCurrentInstance} from "vue";

const baseURL = 'http://localhost:8999/api/v1'

const config = {
    baseURL,
    timeout: 50000
}
const service = axios.create(config)

export default class Api {
    static axiosPost(url = '', data = {}) {
        return new Promise(function (resolve, reject) {
            service({
                method: 'post',
                url,
                data,
                // params,
                paramsSerializer: params => {
                    return qs.stringify(params, {indices: false})
                }
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    resolve(err)
                })
        })
    }

    static axiosPut(url = '', data = {}) {
        return new Promise(function (resolve, reject) {
            service({
                method: 'put',
                url,
                data,
                // params,
                paramsSerializer: params => {
                    return qs.stringify(params, {indices: false})
                }
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    resolve(err)
                })
        })
    }

    static axiosGet(url = '', params = {}) {
        return new Promise(function (resolve, reject) {
            service({
                method: 'get',
                url,
                // data,
                params,
                paramsSerializer: params => {
                    return qs.stringify(params, {indices: false})
                }
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    resolve(err)
                })
        })
    }

    static axiosDelete(url = '', params = {}) {
        return new Promise(function (resolve, reject) {
            service({
                method: 'delete',
                url,
                // data,
                params,
                paramsSerializer: params => {
                    return qs.stringify(params, {indices: false})
                }
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    resolve(err)
                })
        })
    }

    static axiosPatch(url = '', params = {}) {
        return new Promise(function (resolve, reject) {
            service({
                method: 'patch',
                url,
                // data,
                params,
                paramsSerializer: params => {
                    return qs.stringify(params, {indices: false})
                }
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    resolve(err)
                })
        })
    }

    static axiosDownLoad(url = '', params = {}, type = 'post') {
        return new Promise(function (resolve, reject) {
            service({
                method: type,
                url,
                // data,
                params,
                paramsSerializer: params => {
                    return qs.stringify(params, {indices: false})
                },
                responseType: 'blob'
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    resolve(err)
                })
        })
    }
}
// 添加请求拦截器
service.interceptors.request.use(
    function (config) {
        // 在发送请求之前做些什么
        // config.url = config.url
        let user_token = localStorage.getItem("user_token")
        config.headers = {
            // WATER_ADMIN_TOKEN: localStorage.getItem('WATER_ADMIN_TOKEN') || '',
            Authorization: localStorage.getItem("access_token"),
            user_token: user_token === null ? '': user_token
        }
        return config
    },
    function (error) {
        // 对请求错误做些什么
        return Promise.reject(error)
    }
)
//function (response) {
//         // 对响应数据做点什么
//         let resultData=response.data
//         response.data = Object.assign(
//             {},
//             {res: resultData},
//             {status: resultData.code}
//         )
//         return response
//     },
// 添加响应拦截器
service.interceptors.response.use(response => {
        response.data = Object.assign(
            {},
            {res: response.data},
            {status: response.data.code}
        )
        if (response.data.status !== 0){
            if (response.data.status===401){
                // localStorage.removeItem("user_token")
                // window.location.href="/login"
            }
        }
        return response
    },
    function (err) {
        let resData = {
            message: '',
            status: 0
        }
        // 对响应错误做点什么
        if (err && err.response) {
            switch (err.response.status) {
                case 400:
                    resData.message = '错误请求'
                    resData.status = 400
                    break
                case 401:
                    resData.message = '未授权，请重新登录'
                    // window.location.href = '/login'
                    resData.status = 401
                    setTimeout(() => {
                        window.location.href = '/login'
                    }, 1000)
                    break
                case 403:
                    resData.message = '拒绝访问'
                    resData.status = 403
                    break
                case 404:
                    resData.message = '请求错误,未找到该资源'
                    resData.status = 404
                    break
                case 405:
                    resData.message = '请求方法未允许'
                    resData.status = 405
                    break
                case 408:
                    resData.message = '请求超时'
                    resData.status = 408
                    break
                case 500:
                    resData.message = '服务器端出错'
                    resData.status = 500
                    break
                case 501:
                    resData.message = '网络未实现'
                    resData.status = 501
                    break
                case 502:
                    resData.message = '网络错误'
                    resData.status = 502
                    break
                case 503:
                    resData.message = '服务不可用'
                    resData.status = 503
                    break
                case 504:
                    resData.message = '网络超时'
                    resData.status = 504
                    break
                case 505:
                    resData.message = 'http版本不支持该请求'
                    resData.status = 505
                    break
                default:
                    resData.message = `连接错误${err.response.status}`
            }
        } else {
            resData.message = '连接到服务器失败'
        }
        resData.data = err.response
        return Promise.resolve(resData)
    }
)
